<?php
/*
 * inc-addSource.php
 * Retral/CSCD494 Winter 2009
 * Eastern Washington University
 *
 * File Description:
 * Used when user wants to add/edit source from question.php
 * 
 * Other:
 * 
 */

 
/* 
 * Set up $page_name
 * This name will be compared to PAGE_NAME in NAV_URLS table (stored in
 * session) and verify permission for given user.
 */
 
$page_name = "Exams";
$page_name = strtoupper($page_name);

$pm = $_SESSION['pagePermissions'];

// Is user logged in, and has permissions?
if (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] !="" && $_SESSION['pagePermissions'][$page_name] == 1)
{
?>
	<link rel='stylesheet' href="css/preview.css" type="text/css" media="screen">
	<script type="text/javascript" language="javascript" src="script/editQuestion.js"></script>
	<link rel='stylesheet' 
	href="css/code-bright.css" type="text/css" media="screen">
	<form action="<?php echo $_SERVER['PHP_SELF'] . '?page=addSource&qid=' . $_REQUEST['qid'] . '&ver=' . $_REQUEST['ver'] . '&kin=' . $_REQUEST['kin']; ?>" method="POST" enctype="multipart/form-data" >

	<?PHP
		//require('class.highlighter.php');
		require('KinID.php');	
		
		$conn = oci_connect(ORA_CON_UN, ORA_CON_PW, ORA_CON_DB);
		$id = $_REQUEST['qid'];
		$ver = $_REQUEST['ver'];
		$kinID = $_REQUEST['kin'];
		

		if(isset($_POST['submitUpload']))
		{
			if(!empty($_FILES["source"]["name"]))
			{
				if($kinID == "")
				{
					$kin = $_POST['fileType'];
					//if(!isset($_POST['check']))
					//	$kin = class_file(); // Non Java File added as source
				}
				else
				{
					$kin = $kinID;
				}
				
				$query = 
					"INSERT INTO source_file (source_file_id, question_id, q_version, kin, source_file_name, source_file) ".
					"VALUES (source_file_s1.nextval, :qid, :ver, :kin, :file_name, EMPTY_BLOB()) ". 
					"RETURNING source_file INTO :sourceFile ";
					
					$lob_source_file = oci_new_descriptor($conn, OCI_D_LOB);   

					$stmt = oci_parse($conn, $query);
					oci_bind_by_name($stmt, ':qid', $id);
					oci_bind_by_name($stmt, ':ver', $ver);
					oci_bind_by_name($stmt, ':kin', $kin);
					oci_bind_by_name($stmt, ':file_name', $_FILES['source']['name']); 
					oci_bind_by_name($stmt, ':sourceFile', $lob_source_file, -1, OCI_B_BLOB);
					oci_execute($stmt, OCI_DEFAULT);
					
					if($lob_source_file->savefile($_FILES['source']['tmp_name']))
					{
						oci_commit($conn);
						oci_free_statement($stmt);
						print "<p><font color='#00FF00'>Uploaded New File Successfully! </font></p><br><br>";
					}
					else
						echo "<script type=\"text/javascript\"> alert(\"ERROR!.\"); </script>"; 
					print "<a href='?page=question&id=" . $id . "&ver=" . $ver . "'>Back to Edit/View Question</a><br>";
			}
			else
			{
				print "<p><font color='#FF0000'> Unable to upload new file! Please make sure you specify a file. </font></p><br>";
				print "<a href='?page=addSource&qid=" . $_REQUEST['qid'] . "&ver=" . $_REQUEST['ver'] . "'>Try Again</a><br>";
			}    
		}
		else
		{
			print "<h3> Adding a New Source File </h3><br>";
			print '<table>' .
				  '  <tr> ' .
				  '      <td> Specify New File: </td> '.           
				  '      <td><input type="file" name="source" class="required" value=""> </td> ' .
				  '      <td> ';   
            
			if($_REQUEST['kin'] == "")
				print '             <select name="fileType"> ' .
					  '             <option value=' . source_file() .  '> Java File</option> ' .
					  '             <option value=' . class_file()  .  '> Class File</option> ' . 
					  '             <option value=' . provided_io() .  '> Provided Input </option> ' . 
					  '             <option value=' . provided_io() .  '> Provided Output</option> ' .
					  '             </select> ';
            print '       </td>' .
				  '</tr> ' .
				  '</table>';
			print '<input type="submit" value="Go" name="submitUpload" id="submit"> <br> <br>';
			// Set $minVal = 5 if not set, otherwise use current val
			if (!isset($_REQUEST['min']))
				$minVal = 5;
			else
				$minVal = $_REQUEST['min'];
		
			print "<a href='?page=question&id=" . $id . "&ver=" . $ver . "&min=$minVal'>Back to Edit/View Question</a><br>";
		}
		
		
		
	?>
	</form>
	
<?php
} //end if logged in
else
{
	echo "Invalid credentials";
	echo "<META http-equiv=\"refresh\" content=\"0;URL=index.php?page=login&ld=0\">";
}	
?>
